package org.jeecg.modules.system.util;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.util.RestUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class RMSUtil {

    @Autowired
    private Environment environment;

    /**
     * 搬运货架到工作站
     * @param stationId
     * @param shelfCode
     * @return
     */
    public JSONObject deliverShelfToStation(Integer stationId, String shelfCode) {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "RobotTaskRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("taskType", "DELIVER_SHELF_TO_STATION");
        body.put("instruction", "GO_FETCH");
        body.put("stationId", stationId);
        body.put("shelfCode", shelfCode);
        body.put("neededSides", "[F]");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }

    /**
     * 搬运货架到点位
     * @param shelfCode
     * @param destCellCode
     * @return
     */
    public JSONObject deliverShelf(String shelfCode, String destCellCode) {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "RobotTaskRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("taskType", "DELIVER_SHELF");
        body.put("shelfCode", shelfCode);
        body.put("instruction", "GO_FETCH");
        body.put("destCellCode", destCellCode);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }

    /**
     * 查询所有单元格信息
     * @return
     */
    public JSONObject cellPoint() {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "QueryInstructionRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("instruction", "CELL");
        body.put("queryType", "simple");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }

    /**
     * 查询所有工作站信息
     * @return
     */
    public JSONObject cellStationPoint() {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "QueryInstructionRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("instruction", "STATION");
        body.put("queryType", "simple");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }

    /**
     * 货架入场
     * @return
     */
    public JSONObject enterShelf(String shelfCode, String locationCellCode, String placementCellCode) {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "WarehouseInstructionRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("shelfCode", shelfCode);
        body.put("instruction", "ENTER_SHELF");
        body.put("locationCellCode", locationCellCode);
        body.put("placementCellCode", placementCellCode);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }

    /**
     * 移除货架
     * @return
     */
    public JSONObject removeShelf(String shelfCode) {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "WarehouseInstructionRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("shelfCode", shelfCode);
        body.put("instruction", "REMOVE_SHELF");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }

    //获取头信息
    @NotNull
    private JSONObject getHeaders() {
        JSONObject httpHeaders = new JSONObject();// 获取头信息
        httpHeaders.put("requestId", "{{$guid}}");
        httpHeaders.put("clientCode", "wmsCode");
        httpHeaders.put("warehouseCode", "wmsWarehouseCode");
        httpHeaders.put("userId", environment.getProperty("rms.username"));
        httpHeaders.put("userKey", environment.getProperty("rms.password"));
        httpHeaders.put("language", "en_us");
        httpHeaders.put("version", "3.3.0");
        return httpHeaders;
    }
}
